package com.longcai.piaojia.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.conn.LoginPost;
import com.longcai.piaojia.fragment.MineFragment;
import com.longcai.piaojia.util.MEditTextListener;
import com.longcai.piaojia.util.MyToast;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilMatches;

import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

/**
 * Created by Administrator on 2016/8/1.
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener {

    @BoundView(R.id.login_check_sel_iv)
    private ImageView login_check_sel_iv;

    @BoundView(R.id.login_user_account_et)
    private EditText login_user_account_et;

    @BoundView(R.id.login_user_password_et)
    private EditText login_user_password_et;

    @BoundView(R.id.login_forget_password_ll)
    private LinearLayout login_forget_password_ll;

    @BoundView(R.id.login_sumbit_tv)
    private TextView login_sumbit_tv;

    @BoundView(R.id.login_banner_iv)
    private ImageView login_banner_iv;

    private boolean isAfterGoodsInfo = false;



 //   String regId = "123";
    /*  JPushInterface.setAlias(LoginActivity.this, regId,
                 new TagAliasCallback() {
             @Override
             public void gotResult(int arg0, String arg1, Set<String> arg2) {
                 Log.e("JP", "setAlias " + arg0);
                 if (arg0 == 0) {
                 }
             }
         }); */
    private LoginPost loginPost = new LoginPost(new AsyCallBack<LoginPost.LoginPostEntity>() {

        @Override
        public void onSuccess(String toast, int type, LoginPost.LoginPostEntity s) throws Exception {
            super.onSuccess(toast, type, s);

            BaseApplication.BasePreferences.saveUserId(s.user_id);

            BaseApplication.BasePreferences.saveUserAccount(login_user_account_et.getText().toString().trim());

            BaseApplication.BasePreferences.saveIsFavourable(s.group_id);

            BaseApplication.BasePreferences.saveIsLogin(true);

            Toast.makeText(LoginActivity.this, toast, Toast.LENGTH_LONG).show();


            finish();

            MineFragment.getUserAdvert getUserAdvert = (MineFragment.getUserAdvert) getAppCallBack(MineFragment.class);
            getUserAdvert.refreshInfo();

            if (isAfterGoodsInfo) {

                GoodsInfoActivity.RefreshGoodsInfo refreshGoodsInfo = (GoodsInfoActivity.RefreshGoodsInfo) getAppCallBack(GoodsInfoActivity.class);
                refreshGoodsInfo.refresh();
            }


        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            super.onFail(toast, type);

            Toast.makeText(LoginActivity.this, toast, Toast.LENGTH_LONG).show();

        }
    });


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_login);

        setBackTrue();

        setTitleName(getResources().getString(R.string.login_title_text));

        setRightName(getResources().getString(R.string.register_title_text), R.color.white, this);
        JPushInterface.init(LoginActivity.this);
      final String regId = JPushInterface.getRegistrationID(getApplicationContext());
//        final String regId ="123";
        Log.d(regId,"aaaaaaaaaaaaaaa");

        if (JPushInterface.isPushStopped(LoginActivity.this)) {
            JPushInterface.resumePush(LoginActivity.this);//恢复极光推送
        }
        JPushInterface.setAlias(LoginActivity.this, regId, new TagAliasCallback() {
            @Override
            public void gotResult(int i, String s, Set<String> set) {
                Log.e("JP", "setAlias " + i);
                Log.d("jpush",s);

            }
        });
        isAfterGoodsInfo = getIntent().getBooleanExtra("isAfterGoodsInfo", false);

        login_forget_password_ll.setOnClickListener(this);
        login_sumbit_tv.setOnClickListener(this);
        login_banner_iv.setOnClickListener(this);

        login_user_password_et.addTextChangedListener(new MEditTextListener(context, 16, login_user_password_et));

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.login_banner_iv:

                Bundle bundle = new Bundle();

                bundle.putString("url", "http://api.heiyoupiao.com/index.php/share/banner/before");
                bundle.putString("title", "惠民卡");

                startActivity(new Intent(LoginActivity.this, WebActivity.class).putExtras(bundle));

                break;
            case R.id.title_right_ll:

                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));

                break;

            case R.id.login_forget_password_ll:

                startActivity(new Intent(LoginActivity.this, FindPasswordActivity.class));

                break;

            case R.id.login_sumbit_tv:

                String phone = login_user_account_et.getText().toString().trim();
                String password = login_user_password_et.getText().toString().trim();

                if (TextUtils.isEmpty(phone)) {

                    MyToast.show(context, getString(R.string.login_user_account_hint), Toast.LENGTH_LONG);

                    return;

                } else if (!UtilMatches.checkMobile(phone)) {

                    MyToast.show(context, getString(R.string.warning_please_fill_true_phone_text), Toast.LENGTH_LONG);

                    return;

                } else if (TextUtils.isEmpty(password)) {

                    MyToast.show(context, getString(R.string.login_user_password_hint), Toast.LENGTH_LONG);

                    return;

                }

                loginPost.phone = phone;
                loginPost.user_pass = password;
                loginPost.execute(LoginActivity.this);

                break;

        }

    }
}
